// subPages/equipment/sleepReport/sleepReport.js
const app = getApp();
var util = require('../../../utils/util');
var utils =  require('../../../utils/healthUtil.js');
var json =  require('../../../data/json.js');
let PieChart = null
import * as echarts from '../../../ec-canvas/echarts';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      // lazyLoad: true // 延迟加载
    },
    sleepList: [
      {
        text: '醒着',
        value: '',
        color: '#eb6559'
      },
      {
        text: '深睡',
        value: '',
        color: '#7592ec'
      },
      {
        text: '浅睡',
        value: '',
        color: '#f9e14b'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.date){
      console.log(options.date)
      this.getData(options.date)
    }
  },

  //获取数据
  getData:function(date){
    var that = this
    util.getAjax(`${app.globalData.apiUrl}/Api/Sleep/getreportbydevid`,function(res){
      console.log(res)
      if(res.data && res.data.data.length!=0){
        var list = res.data.data
        var pieList = []
        pieList.push(list[0].sober,list[0].deepSleep,list[0].lightSleep)
        var sleepList = that.data.sleepList
        for(var i in sleepList){
          switch (sleepList[i].text) {
            case '醒着':
              sleepList[i].value = list[0].sober
              break;
            case '深睡':
              sleepList[i].value = list[0].deepSleep
              break;
            case '浅睡':
              sleepList[i].value = list[0].lightSleep
              break;
          }
        }
        var sleepReportList = json.sleepReportList
        for(var j in sleepReportList){
          switch (sleepReportList[j].index) {
            case 1:
              sleepReportList[j].data = list[0].startBedtime
              break;
            case 2:
              sleepReportList[j].data = list[0].endBedtime
              break;
            case 3:
              sleepReportList[j].data = list[0].sleepTime
              break;
            case 4:
              sleepReportList[j].data = list[0].sleepHours + '分钟'
              break;
            case 5:
              sleepReportList[j].data = list[0].sleepQuality 
              break;
            case 6:
              sleepReportList[j].data = list[0].heartRate + '次/分'
              break;
            case 7:
              sleepReportList[j].data = list[0].respiratoryRate + '次/分'
              break;
            case 8:
              sleepReportList[j].data = list[0].bodyMove + '次'
              break;
            case 9:
              sleepReportList[j].data = list[0].bedAway + '次'
              break;
            case 10:
              sleepReportList[j].data = list[0].abnormal + '次'
              break;
          }
        }
        that.setData({
          isSleep: true,
          pieList: pieList,
          sleepList: sleepList,
          sleepReportList: sleepReportList,
          advice: list[0].advice
        })
        that.piechartsComponnet = that.selectComponent('#mychartPie');
        PieChart = null
        that.getPieGraphData();  //创建图表
      }else{
        that.setData({
          isSleep: false,
        })
      }
    },{date: date})
  },

  //线图表
  getPieGraphData:function(){
    var that = this
    if (!PieChart){
      that.init_pieEcharts(); //初始化图表
    }else{
      that.setPieOption(); //更新数据
    }
  },

  //初始化图表
  init_pieEcharts: function () {
    var that = this
    console.log(that.piechartsComponnet)
    that.piechartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      PieChart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      PieChart.showLoading({
        text : '正在努力的读取数据中...'
      });
      // Chart.setLineOption(this.getLineOption());
      that.setPieOption();
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return PieChart;
    });
  },

  setPieOption: function () {
    PieChart.clear();  // 清除
    PieChart.setOption(this.getPieOption());  //获取新数据
  },

  //设置折线图表
  getPieOption: function () {
    var that = this
    // 指定图表的配置项和数据
    var option = {
      color:['#eb6559','#7592ec','#f9e14b'],
      series : [{
        name: '',
        type: 'pie',
        radius : '90%',
        center: ['50%', '50%'],
        labelLine: {
          show: false,
        },
        data:that.data.pieList,
        
      }]
    };
    PieChart.hideLoading()
    console.log(PieChart)
    return option;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})